using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmDsBnChuyenDi : XtraForm
  {
    private DataTable _dtbDsBenhNhan;

    public FrmDsBnChuyenDi()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmDsBnChuyenDi_Load(object sender, EventArgs e)
    {
      LoadData();
    }

    #region Command Handlers

    private void btnClear_Click(object sender, EventArgs e)
    {
      UIUtils.ClearOtherEdit(deTuNgay);
      UIUtils.ClearOtherEdit(deDenNgay);
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
      pceSearch.ClosePopup();
      LoadData();
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {

    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {
        DateTime? tuNgay = (deTuNgay.EditValue != null) ?
          (DateTime?)deTuNgay.EditValue : null;
        DateTime? denNgay = (deDenNgay.EditValue != null) ?
          (DateTime?)deDenNgay.EditValue : null;

        DataSet dtsDsBenhNhan = null;
        //= GlobalCache.LichSuDieuTriService.DsBenhNhanChuyenDi(GlobalCache.Permission.UnitCode, tuNgay, denNgay);
        _dtbDsBenhNhan = dtsDsBenhNhan.Tables[0];
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmDsBnChuyenDi::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();

      if (_dtbDsBenhNhan != null)
      {
        gcDsBenhNhan.DataSource = _dtbDsBenhNhan;
        MainFormUtils.SetLoadedStatus();
      }
      else
      {
        MainFormUtils.SetSavedFailedStatus();
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion DataLoading BackgroundWorker

    private void edit_Enter(object sender, EventArgs e)
    {
      UIUtils.SelectAll(sender as BaseEdit);
    }

    #endregion

    #region Helper Methods

    private void LoadData()
    {
      DateTime today = DateTime.Today;
      DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
      DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1);
      lastDayOfMonth = lastDayOfMonth.Subtract(new TimeSpan(1, 0, 0, 0));

      deTuNgay.DateTime = firstDayOfMonth;
      deDenNgay.DateTime = lastDayOfMonth;

      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      bwDataLoading.RunWorkerAsync();
    }

    #endregion
  }
}